package com.sztx.pay.center.tool;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSONObject;
import com.sztx.commons.httpclient.HttpClientConfigBuilder;
import com.sztx.commons.httpclient.HttpClientUtil;
import com.sztx.commons.httpclient.HttpHeaderBuilder;

public class RollBackChargeFee {
	private static final Logger logger = LoggerFactory.getLogger(GrayUsers.class);	
	private static final Logger rollBacklogger = LoggerFactory.getLogger("rollBackChargeFee");

	public static String ROLL_BACK_URL = "http://cashier.berbon.com/trade/transfer?amount=%s&payerUserId=1001167016&payeeUserId=%s&srcChannel=0&payType=1&receiverType=1&channelId=99&businessType=2014&attach=手续费退款";
	//public static String ROLL_BACK_URL = "http://dev.cashier.berbon.com/trade/transfer?amount=%s&payerUserId=1000280005&payeeUserId=%s&srcChannel=0&payType=1&receiverType=1&channelId=99&businessType=2014&attach=手续费退款";
	public static String SESSION_USER_URL = "http://cms.berbon.com/support/channel/getGroupList?d=%7b%22pageNo%22%3a1%2c%22pageSize%22%3a1%7d";
		
	public static void main(String[] args) throws IOException {
		BufferedReader bf = new BufferedReader(new FileReader("E:/文档/rollBackChargeFee20160908-20160910.txt"));
		//BufferedReader bf = new BufferedReader(new FileReader("E:/文档/rollBackChargeFeeTest.txt"));
		
		int totalAmount = 0;
		String readLine = null;
		int i = 0;
		long lastVisitTime = 0;
		List<String> errUserIds = new ArrayList<String>();
		while ((readLine = bf.readLine()) != null) {// 使用readLine方法，一次读一行			
			long currentTime = System.currentTimeMillis();
			if (currentTime - lastVisitTime > 60000) {  //5分钟访问一次session								
				String result = getHtmlContent(SESSION_USER_URL);				
				if (result == null || !result.startsWith("{\"code\":\"J000000\"")) { //session未访问成功
					logger.info("访问session结果失败{}", result);
					System.exit(0);
				}				
				lastVisitTime = currentTime;
			}
			
			readLine = readLine.trim();
			i++;		
			
			String[] readLineArr = readLine.split("\\|");
			double amountD = Double.parseDouble(readLineArr[4]) * 100;
			if(amountD < 0){
				continue;
			}
			int amount = (int)amountD;
			String userId = readLineArr[6];
			
			
			
			
			
			
			String url = String.format(ROLL_BACK_URL, amount,userId);
			
			
			String result = getHtmlContent(url);
			String code = null;
			JSONObject parseObject = null;
			if (result != null && !result.isEmpty()) {
				parseObject = JSONObject.parseObject(result);
				code = parseObject.getString("code");
			}			
			
			String resultMsg = "【成功】";
			if ("J000997".equals(code)) {
				String errMsg = parseObject.getString("description");
				errUserIds.add(readLine);
				rollBacklogger.error("转账第{}条{}失败,失败原因{}", i, readLine, errMsg);
			}else {
				totalAmount += amount;
				rollBacklogger.info("转账第:" + i + "行,订单信息【" + readLine + "】," + resultMsg);				
			}
			
		}
		rollBacklogger.info("转账失败列表:" + JSONObject.toJSONString(errUserIds));
		rollBacklogger.info("转账总金额:"+(double)totalAmount/100+"元");
		
	}
	
	
	private static String getHtmlContent(String url){		
		HttpClientConfigBuilder clientBuilder = HttpClientConfigBuilder.custom();
		HttpHeaderBuilder headerBuilder = HttpHeaderBuilder.custom().cookie("berbonjsessionid=8A142813DE06483AB1F1793B5E397405");		
		String encoding = "UTF-8";
		return HttpClientUtil.get(clientBuilder, url, headerBuilder, encoding);	
	}
	
	
	
}
